package com.dongdongsongcan.app.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.control.FavouriteControl;
import com.dongdongsongcan.app.control.bean.ShopData;
import com.dongdongsongcan.app.control.bean.ShopData.Shop;
import com.su.dolphin.adapter.DpBaseAdapter;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.core.proxy.ImageLoader;
import com.su.dolphin.utils.ui.TipsUtil;

public class ShoplistAdapter extends DpBaseAdapter<Shop> {

    public ShoplistAdapter(Context context) {
        super(context);
    }

    @Override
    public int onGetItemLayout() {
        return R.layout.layout_shop_item;
    }

    @Override
    public void onGetView(View convertView, int position) {
        ImageView shop_item_icon = findViewById(convertView,
                R.id.shop_item_icon);
        TextView shop_item_name = findViewById(convertView, R.id.shop_item_name);
        TextView shop_item_status = findViewById(convertView,
                R.id.shop_item_status);
        TextView shop_item_fee = findViewById(convertView, R.id.shop_item_fee);
        TextView shop_item_address = findViewById(convertView,
                R.id.shop_item_address);
        final ImageView shop_item_fav = findViewById(convertView, R.id.shop_item_fav);

        final Shop shop = mDataList.get(position);

        ImageLoader.i().loadImage(shop_item_icon, shop.getShop_pic_small());
        shop_item_name.setText(shop.getShop_name());
        shop_item_status.setText(shop.getShopStatus() + "");
//        shop_item_fee.setText("起送 " + shop.getShop_dispatch_price() + "￥ | 配送" + shop.getShop_addprice() + "￥| " + 60 + "分钟送达");
        shop_item_fee.setText("起送 " + shop.getShop_dispatch_price() + "￥ | 60" + "分钟送达");

        shop_item_address.setText("地址: " + shop.getShop_address());
        if (shop.getShop_collect() == 1) {
            shop_item_fav.setImageResource(R.drawable.main_fav_shop_ic_checked);
        } else if (shop.getShop_collect() == 0) {
            shop_item_fav.setImageResource(R.drawable.main_fav_shop_ic);
        } else {
            shop_item_fav.setVisibility(View.GONE);

        }

        shop_item_fav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shop.getShop_collect() == 1) {
                    FavouriteControl.i().user_collect_del(shop.getShop_id(), new CtrlTask.IControlResponse<ShopData>() {
                        @Override
                        public void onControlResponse(ShopData data) {
                            TipsUtil.toast(data.getMsg());
                            if (data.isOk()) {
                                shop.setShop_collect(0);
                                shop_item_fav.setImageResource(R.drawable.main_fav_shop_ic);
                            }
                        }
                    });

                } else {
                    FavouriteControl.i().user_collect_insert(shop.getShop_id(), new CtrlTask.IControlResponse<ShopData>() {
                        @Override
                        public void onControlResponse(ShopData data) {
                            TipsUtil.toast(data.getMsg());
                            if (data.isOk()) {
                                shop.setShop_collect(1);
                                shop_item_fav.setImageResource(R.drawable.main_fav_shop_ic_checked);
                            }
                        }
                    });
                }

            }
        });
    }
}
